package com.js.auth.server.module.client.controller;

import com.js.auth.server.config.KeyConfig;
import com.js.auth.server.module.client.service.AuthClientService;
import com.js.common.response.ObjectRestResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author ygl
 * @create 2018-10-31
 * @DESCRIPTION
 * 提供给客户端的接口
 **/
@Slf4j
@RestController
@RequestMapping("client")
public class ClientController {

    @Autowired
    private AuthClientService authClientService;

    @Autowired
    private KeyConfig keyConfig;

    /**
     * 通过客户端信息获取认证服务器提供的校验用户token的公钥
     * @param clientId 客户端ID
     * @param secret   客户端秘钥
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/userPubKey")
    public ObjectRestResponse<byte[]> getUserPublicKey(@RequestParam("clientId") String clientId, @RequestParam("secret") String secret) throws Exception {
        authClientService.validate(clientId, secret);
        log.info("-------"+clientId+"客户端(微服务)来获取用户公钥---------");
        return new ObjectRestResponse<byte[]>().data(keyConfig.getUserPubKey());
    }
}
